/*
 * Created on 2004-07-09
 */
package org.opencube.oms.mapping;

import java.util.HashMap;
import java.util.Properties;

import org.opencube.data.BoundVariable;
import org.opencube.data.DataSource;
import org.opencube.oms.OMSData;
import org.opencube.oms.OMSElement;
import org.opencube.oms.OMSStructure;

/**
 * @author <a href="mailto:maciek@fingo.pl">Maciej Mroczko - FINGO</a>
 *
 * TODO comments
 */
public class DataStore
{
	/* The underlying oms data object */
	private OMSData data = null;

	public DataStore( String applicationResourcesPath, String connectionAlias, int userId )
	throws Exception
	{
		this( new OMSData(  applicationResourcesPath, connectionAlias, userId ) );
	}
	
	public DataStore( DataSource dataSource, int userId )
	throws Exception
	{
		this( new OMSData(  dataSource, userId ) );
	}

	public DataStore( DataSource dataSource, String userBookmark )
	throws Exception
	{
		this( new OMSData(  dataSource, 0 ) );
		updateUserIdByBookmark( userBookmark );
	}

	public DataStore( String applicationResourcesPath, String connectionAlias, String userBookmark )
	throws Exception
	{
		this( new OMSData(  applicationResourcesPath, connectionAlias, 0 ) );
		updateUserIdByBookmark( userBookmark );
		
	}

	public DataStore( String applicationResourcesPath, Properties dsPoperties, int userId )
	throws Exception
	{
		this( new OMSData( applicationResourcesPath, dsPoperties, userId ) );
	}
	
	public DataStore( String applicationResourcesPath, Properties dsPoperties, String userBookmark )
	throws Exception
	{
		this( new OMSData( applicationResourcesPath, dsPoperties, 0 ) );
		updateUserIdByBookmark( userBookmark );
	}

	public DataStore( Properties dsProperties, int userId )
	throws Exception
	{
		this( null, dsProperties, userId );
	}
	
	public DataStore( Properties dsProperties, String userBookmark )
	throws Exception
	{
		this( null, dsProperties, 0 );
		updateUserIdByBookmark( userBookmark );
	}

	public DataStore( OMSData data )
	{
		this.data = data;
	}
	
	public void updateUserIdByBookmark( String userBookmark )
	throws Exception
	{
		OMSElement user = new Finder( this ).findElementByBookmark( userBookmark, false );
		if( user == null )
		{
			throw new IllegalArgumentException( "User with bookmark '" + userBookmark + "' not found" );
		}
		this.data.setUserId( Integer.parseInt( user.getId() ) );
	}
	
	public void store( Entity entity ) throws Exception
	{
		data.saveOMSStructure( entity.getElement().getOMSStructure() );
	}
	
	OMSStructure getStructureByWhere( String where )
	throws Exception
	{
		return data.getOMSStructureByWhere( where );
	}
	
	OMSStructure getStructureByResource( String resource, HashMap placeHolders, BoundVariable[] bindVariables )
	throws Exception
	{
		return data.getOMSStructureByResource( resource, placeHolders, bindVariables );
	}

	public void commit() throws Exception
	{
		data.getDataSource().commit();
	}
	
	public void rollback() throws Exception
	{
		data.getDataSource().rollback();
	}
	
	public OMSData getOMSData()
	{
		return data;
	}
}
